/*
	Base functions for voxels.
*/

#include "../../../Shared/data/cl/base.cl"


//------------------------------------------------------------------------------//
//									HEADER										//

enum ERgbaColor
{
	COLOR_BLACK	= 0x000000FF,
	COLOR_WHITE	= 0xFFFFFFFF,
	COLOR_GREEN	= 0x00FF00FF,
	COLOR_RED	= 0xFF0000FF,
	COLOR_BLUE	= 0x0000FFFF,
};

int2 ToHeightMapCoords (int3 globalCoord);
void WriteToHeightMap (write_only image2d_t image, int2 coord, float height, uint color);

float UintToFloatBits (uint value);
uint FloatToUintBits (float value);

uint RgbaToAbgr (uint color);


//------------------------------------------------------------------------------//
//									SOURCE										//

int2 ToHeightMapCoords (int3 globalCoord)
{
	enum {
		HM_BORDER = 2,
	};

	int2 c = globalCoord.xy + HM_BORDER;
	return c;
}

void WriteToHeightMap (write_only image2d_t image, int2 coord, float height, uint color)
{
	float4	data;
	data.x = height;
	data.y = UintToFloatBits( RgbaToAbgr( color ) );

	write_imagef( image, coord, data );
}

float UintToFloatBits (uint value)
{
	union {
		float f;
		uint  u;
	} data;
	data.u = value;
	return data.f;
}

uint FloatToUintBits (float value)
{
	union {
		float f;
		uint  u;
	} data;
	data.f = value;
	return data.u;
}

uint RgbaToAbgr (uint color)
{
	union {
		uchar4	c;
		uint	u;
	} rgba, abgr;

	rgba.u = color;
	abgr.c.x = rgba.c.w;
	abgr.c.y = rgba.c.z;
	abgr.c.z = rgba.c.y;
	abgr.c.w = rgba.c.x;

	return abgr.u;
}
